
import cron_base

from django.db import models
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.template import Context, loader
from django.conf import settings
import datetime

from freedel.conspire.models import UserProfile

import smtplib

def send_activation_mail(userprofilex):

    email_subject = 'Confirm your FreeDel \'07 Registration'
    current_domain = Site.objects.get_current().domain
            
    t = loader.get_template('registration_mail.txt')
    c = Context({'full_name' : userprofilex.get_full_name(),
                 'base_url' : current_domain,
                 'activation_key' : userprofilex.activation_key
                 })

    email_body = t.render(c)

    send_mail(email_subject,
              email_body,
              settings.EMAIL,
              [userprofilex.user.email])

    
if __name__ == '__main__':

    inactive_users = UserProfile.objects.\
                     exclude(user__is_active__exact = True).\
                     filter(key_expires__lt = datetime.datetime.now())
                 
    for up in inactive_users:
        try:
            send_activation_mail(up)
            up.reset_expiry_datetime()
        except smtplib.SMTPException:
            continue


        
        
